package appapi.service.impl;

import appapi.Request.SelfcheckPageReq;
import appapi.Request.record.AnnxPageReq;
import appapi.Vo.record.AnnxPageListVo;
import appapi.dao.AnnexMapper;
import appapi.dao.SelfcheckMapper;
import appapi.entity.Zhongjiao.AnnexDto;
import appapi.entity.bridge.vo.AnnexDataCountVo;
import appapi.entity.bridge.vo.AnnexMouthCountVo;
import appapi.entity.bridge.vo.SafeReq;
import appapi.service.IAnnexService;
import appapi.service.ISelfcheckService;
import com.github.pagehelper.PageHelper;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author djq
 * @date 2023/5/18 17:03
 * @description:
 */

@Component
public class AnnexServiceImpl implements IAnnexService {
    @Resource
    private AnnexMapper annexMapper;

    @Resource
    private SelfcheckMapper selfcheckMapper;


    @Override
    public List<Map<String, Object>> SelectAnnexList(Long selfId, String annexType) {
        return annexMapper.selectAnnexs(selfId,annexType);
    }

    @Override
    public void InsertAnnex(AnnexDto dto) {
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());
        annexMapper.insertSelective(dto);
    }

    @Override
    public void DelAnnex(Long annexId) {
        annexMapper.deleteByPrimaryKey(annexId);
    }

    @Override
    public List<AnnxPageListVo> getList(AnnxPageReq req) {
//        List<AnnexDto> list = annexMapper.selectList();
        //自检
        List<Long> longs = selfcheckMapper.getselfByOrgId(req.getOrgaId());
        List<AnnxPageListVo> section1 = new ArrayList<>();
        for (Long aLong : longs) {
            List<AnnxPageListVo> selfCheck = annexMapper.selecAllByRecIds("selfCheck", aLong);
            section1 = Stream.of(section1, selfCheck).flatMap(Collection::stream).collect(Collectors.toList());
        }
        //巡检
        List<Long> onsiteByOrgId = selfcheckMapper.getOnsiteByOrgId(req.getOrgaId());
        List<AnnxPageListVo> section2 = new ArrayList<>();
        for (Long aLong : onsiteByOrgId) {
            List<AnnxPageListVo> onsiteInspection = annexMapper.selecAllByRecIds("onsiteInspection", aLong);
            section2 = Stream.of(section2, onsiteInspection).flatMap(Collection::stream).collect(Collectors.toList());
        }
        section1.addAll(section2);
        //月检
        List<Long> mouthByOrgId = selfcheckMapper.getMouthByOrgId(req.getOrgaId());
        List<AnnxPageListVo> section3 =new ArrayList<>();
        for (Long aLong : mouthByOrgId) {
            List<AnnxPageListVo> monthCheck = annexMapper.selecAllByRecIds("monthCheck", aLong);
            section3 = Stream.of(section3, monthCheck).flatMap(Collection::stream).collect(Collectors.toList());
        }
        section1.addAll(section3);
        //整改
        List<Long> rectByOrgId = selfcheckMapper.getRectByOrgId(req.getOrgaId());
        List<AnnxPageListVo> section4 = new ArrayList<>();
        for (Long aLong : rectByOrgId) {
            List<AnnxPageListVo> rectification = annexMapper.selecAllByRecIds("rectification", aLong);
            section4 = Stream.of(section4, rectification).flatMap(Collection::stream).collect(Collectors.toList());
        }
        section1.addAll(section4);
        //工段
        List<Long> sectionByOrgId = selfcheckMapper.getSectionByOrgId(req.getOrgaId());
        List<AnnxPageListVo> section5 =  new ArrayList<>();
        for (Long aLong : sectionByOrgId) {
            List<AnnxPageListVo> section = annexMapper.selecAllByRecIds("section", aLong);
            section5 = Stream.of(section5, section).flatMap(Collection::stream).collect(Collectors.toList());
        }
        section1.addAll(section5);

        PageHelper.startPage(req.getPageNum(), req.getPageSize());
//        List<AnnxPageListVo> list = annexMapper.getList();
        return section1;
    }

    @Override
    public List<AnnexDto> selectList(Long orgId) {
//        List<AnnexDto> list = annexMapper.selectList();
        //自检
        List<Long> longs = selfcheckMapper.getselfByOrgId(orgId);
        List<AnnexDto> section1 = new ArrayList<>();
        for (Long aLong : longs) {
            List<AnnexDto> selfCheck = annexMapper.selectAllByRecIds("selfCheck", aLong);
             section1 = Stream.of(section1, selfCheck).flatMap(Collection::stream).collect(Collectors.toList());
        }
        //巡检
        List<Long> onsiteByOrgId = selfcheckMapper.getOnsiteByOrgId(orgId);
        List<AnnexDto> section2 = new ArrayList<>();
        for (Long aLong : onsiteByOrgId) {
            List<AnnexDto> onsiteInspection = annexMapper.selectAllByRecIds("onsiteInspection", aLong);
            section2 = Stream.of(section2, onsiteInspection).flatMap(Collection::stream).collect(Collectors.toList());
        }
        section1.addAll(section2);
        //月检
        List<Long> mouthByOrgId = selfcheckMapper.getMouthByOrgId(orgId);
        List<AnnexDto> section3 =new ArrayList<>();
        for (Long aLong : mouthByOrgId) {
            List<AnnexDto> monthCheck = annexMapper.selectAllByRecIds("monthCheck", aLong);
            section3 = Stream.of(section3, monthCheck).flatMap(Collection::stream).collect(Collectors.toList());
        }
        section1.addAll(section3);
        //整改
        List<Long> rectByOrgId = selfcheckMapper.getRectByOrgId(orgId);
        List<AnnexDto> section4 = new ArrayList<>();
        for (Long aLong : rectByOrgId) {
            List<AnnexDto> rectification = annexMapper.selectAllByRecIds("rectification", aLong);
                section4 = Stream.of(section4, rectification).flatMap(Collection::stream).collect(Collectors.toList());
        }
        section1.addAll(section4);
        //工段
        List<Long> sectionByOrgId = selfcheckMapper.getSectionByOrgId(orgId);
        List<AnnexDto> section5 =  new ArrayList<>();
        for (Long aLong : sectionByOrgId) {
            List<AnnexDto> section = annexMapper.selectAllByRecIds("section", aLong);
            section5 = Stream.of(section5, section).flatMap(Collection::stream).collect(Collectors.toList());
        }
        section1.addAll(section5);
//        List<AnnexDto> collect = Stream.of(section1, section2, section3, section4, section5).distinct().flatMap(Collection::stream).collect(Collectors.toList());
        return section1;
    }

    /**
     * 前12个月数据趋势图
     * @return
     */
    @Override
    public List<AnnexMouthCountVo> getLikeMouth(Long orgId) {
        List<AnnexDto> section1 = new ArrayList<>();
        List<AnnexMouthCountVo>list2=new ArrayList<>();
        //获取当前年月日
        //2023-10-21
        java.time.LocalDate now1 = java.time.LocalDate.now();
        //循环获得对应的前12个月
        for (int i = 12; i >0 ; i--) {
            //i=1
            //date为:  //2023-09-21
            LocalDate date = now1.plusMonths(-i);
            String str = date.toString();
            //str.substring(0,7)只获取年月
            String mouth = str.substring(0, 7);
            //自检
            List<Long> longs = selfcheckMapper.getselfByOrgId(orgId);
            for (Long aLong : longs) {
                List<AnnexDto> selfCheck = annexMapper.selectAllByRecIdss("selfCheck", aLong, mouth);
                section1 = Stream.of(section1, selfCheck).flatMap(Collection::stream).collect(Collectors.toList());
            }
            //巡检
            List<Long> onsiteByOrgId = selfcheckMapper.getOnsiteByOrgId(orgId);
            List<AnnexDto> section2 = new ArrayList<>();
            for (Long aLong : onsiteByOrgId) {
                List<AnnexDto> onsiteInspection = annexMapper.selectAllByRecIdss("onsiteInspection", aLong, mouth);
                section2 = Stream.of(section2, onsiteInspection).flatMap(Collection::stream).collect(Collectors.toList());
            }
            section1.addAll(section2);
            //月检
            List<Long> mouthByOrgId = selfcheckMapper.getMouthByOrgId(orgId);
            List<AnnexDto> section3 = new ArrayList<>();
            for (Long aLong : mouthByOrgId) {
                List<AnnexDto> monthCheck = annexMapper.selectAllByRecIdss("monthCheck", aLong, mouth);
                section3 = Stream.of(section3, monthCheck).flatMap(Collection::stream).collect(Collectors.toList());
            }
            section1.addAll(section3);
            //整改
            List<Long> rectByOrgId = selfcheckMapper.getRectByOrgId(orgId);
            List<AnnexDto> section4 = new ArrayList<>();
            for (Long aLong : rectByOrgId) {
                List<AnnexDto> rectification = annexMapper.selectAllByRecIdss("rectification", aLong, mouth);
                section4 = Stream.of(section4, rectification).flatMap(Collection::stream).collect(Collectors.toList());
            }
            section1.addAll(section4);
            //工段
            List<Long> sectionByOrgId = selfcheckMapper.getSectionByOrgId(orgId);
            List<AnnexDto> section5 = new ArrayList<>();
            for (Long aLong : sectionByOrgId) {
                List<AnnexDto> section = annexMapper.selectAllByRecIdss("section", aLong, mouth);
                section5 = Stream.of(section5, section).flatMap(Collection::stream).collect(Collectors.toList());
            }
            section1.addAll(section5);
            AnnexMouthCountVo annexMouthCountVo = new AnnexMouthCountVo(date, section1.size());
            list2.add(annexMouthCountVo);
        }
//        List<AnnexMouthCountVo>list=new ArrayList<>();
//        //获取当前年月日
//        //2023-10-21
//        java.time.LocalDate now = java.time.LocalDate.now();
//        //循环获得对应的前12个月
//        for (int i = 12; i >0 ; i--) {
//            //i=1
//            //date为:  //2023-09-21
//            LocalDate date = now.plusMonths(-i);
//            String str = date.toString();
//            //str.substring(0,7)只获取年月
//            Integer MouthCount = annexMapper.getLikeMouth(str.substring(0,7));
//            AnnexMouthCountVo annexMouthCountVo = new AnnexMouthCountVo(date, MouthCount);
//            list.add(annexMouthCountVo);
//        }
        return list2;
    }



}
